package cn.edu.ncut.cs.springboot.springsecuritydemo.controller;

import cn.edu.ncut.cs.springboot.springsecuritydemo.entity.Book;
import cn.edu.ncut.cs.springboot.springsecuritydemo.service.BookService;
import cn.edu.ncut.cs.springboot.springsecuritydemo.util.Result;
import cn.edu.ncut.cs.springboot.springsecuritydemo.vo.BookVO;
import cn.edu.ncut.cs.springboot.springsecuritydemo.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * ClassName : BookController
 * package : cn.edu.ncut.cs.springboot.springsecuritydemo.controller
 * Description :
 *
 * @ Author Dreama
 * @ Creat 2024/12/24 13:41
 * @ Version 1.0
 */
/*
    @GetMapping("/view/{id}")
    public JsonResult getUserInfo(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setUsername(user.getUsername());
        userVO.setPassword(user.getPassword());
        userVO.setTimestamp(user.getGmtModified().toEpochMilli());
        return new JsonResult(JsonResult.JsonResultCode.SUCCESS, "获取数据成功！", userVO);
    }
 */
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/view/{id}")
    public JsonResult getAllBooks(@PathVariable("id") Long id){
        Book book = bookService.getById(id);
        BookVO bookVO = new BookVO();
        bookVO.setId(book.getid());
        bookVO.setTitle(book.getTitle());
        bookVO.setAuthor(book.getAuthor());
        bookVO.setIsbn(book.getIsbn());
        return new JsonResult(JsonResult.JsonResultCode.SUCCESS,"获取成功！",bookVO);
    }
//    @GetMapping
//    public List<Book> getAllBooks(){
//        return bookService.getAllBooks();
//    }

    @GetMapping("/list")
    public JsonResult getBookList(){
        try {
            List<BookVO> bookList = bookService.findAll();
            return new JsonResult(JsonResult.JsonResultCode.SUCCESS,"获取成功！",bookList);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(JsonResult.JsonResultCode.ERROR,"获取失败！",null);
        }
    }
//    @PostMapping("employees")
//    public Result<Employee> addEmployee(@RequestBody Employee employee) {
//        Result<Employee> r = new Result<>();
//        Employee e = employeeService.saveEmployee(employee);
//        return r.success("新增员工信息成功！", e);
//    }
    @PostMapping("/add")
    public Result<Book> createBook(@RequestBody Book book){
        Result<Book> r = new Result<>() ;
        Book e = bookService.saveBook(book) ;
        return r.success("添加成功",e);
    }
}

/*
    @GetMapping("/list")
    public JsonResult getUserList() {
        try {
            List<UserVO> userList = userService.findAll();
            return new JsonResult(JsonResult.JsonResultCode.SUCCESS, "获取数据成功！", userList);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(JsonResult.JsonResultCode.ERROR, "获取数据失败！", null);
        }
    }
 */